<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="运营者类型" prop="usertype">
        <el-select v-model="queryParams.usertype" placeholder="请选择运营者类型" clearable>
          <el-option v-for="dict in dict.type.camp_usertype_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="营地类型" prop="camptype">
        <el-select v-model="queryParams.camptype" placeholder="请选择营地类型" clearable>
          <el-option v-for="dict in dict.type.camp_camptype_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="营地名称" prop="campname">
        <el-input v-model="queryParams.campname" placeholder="请输入营地名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="营地经度" prop="longitude">
        <el-input v-model="queryParams.longitude" placeholder="请输入营地经度" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="营地纬度" prop="latitude">
        <el-input v-model="queryParams.latitude" placeholder="请输入营地纬度" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="营地位置" prop="location">
        <el-input v-model="queryParams.location" placeholder="请输入营地位置" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="具体位置" prop="address">
        <el-input v-model="queryParams.address" placeholder="请输入具体位置" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="营地时间类型" prop="timeType">
        <el-select v-model="queryParams.timeType" placeholder="请选择营地时间类型" clearable>
          <el-option v-for="dict in dict.type.camp_timetype_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="联系人" prop="linkman">
        <el-input v-model="queryParams.linkman" placeholder="请输入联系人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="营地状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择营地状态" clearable>
          <el-option v-for="dict in dict.type.camp_state_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="approve">
        <el-select v-model="queryParams.approve" placeholder="请选择审核状态" clearable>
          <el-option v-for="dict in dict.type.camp_approve_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否置顶" prop="show">
        <el-select v-model="queryParams.show" placeholder="请选择是否置顶" clearable>
          <el-option v-for="dict in dict.type.camp_show_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="评分" prop="score">
        <el-input v-model="queryParams.score" placeholder="请输入评分" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="具体时间" prop="specificTime">
        <el-date-picker v-model="value2" type="monthrange" align="right" unlink-panels range-separator="至"
          start-placeholder="开始月份" end-placeholder="结束月份" :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['camp:camp:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['camp:camp:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['camp:camp:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['camp:camp:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="campList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="营地Id" align="center" prop="campid" />
      <el-table-column label="运营者类型" align="center" prop="usertype">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.camp_usertype_status" :value="scope.row.usertype" />
        </template>
      </el-table-column>
      <el-table-column label="营地类型" align="center" prop="camptype">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.camp_camptype_status" :value="scope.row.camptype" />
        </template>
      </el-table-column>
      <el-table-column label="营地名称" align="center" prop="campname" />
      <el-table-column label="营地经度" align="center" prop="longitude" />
      <el-table-column label="营地纬度" align="center" prop="latitude" />
      <el-table-column label="营地位置" align="center" prop="location" />
      <el-table-column label="具体位置" align="center" prop="address" />
      <el-table-column label="营地时间类型" align="center" prop="timeType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.camp_timetype_status" :value="scope.row.timeType" />
        </template>
      </el-table-column>
      <el-table-column label="营地具体时间" align="center" prop="specificTime" />
      <el-table-column label="联系人" align="center" prop="linkman" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="图片" align="center" prop="imageUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.imageUrl" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="营地状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.camp_state_status" :value="scope.row.state" />
        </template>
      </el-table-column>
      <el-table-column label="介绍" align="center" prop="introduce" />
      <el-table-column label="审核状态" align="center" prop="approve">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.camp_approve_status" :value="scope.row.approve" />
        </template>
      </el-table-column>
      <el-table-column label="是否置顶" align="center" prop="show">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.camp_show_status" :value="scope.row.show" />
        </template>
      </el-table-column>
      <el-table-column label="评分" align="center" prop="score" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['camp:camp:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['camp:camp:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改营地对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="运营者类型" prop="usertype">
          <el-radio-group v-model="form.usertype">
            <el-radio v-for="dict in dict.type.camp_usertype_status" :key="dict.value" :label="dict.value">{{ dict.label
              }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="营地类型" prop="camptype">
          <el-radio-group v-model="form.camptype">
            <el-radio v-for="dict in dict.type.camp_camptype_status" :key="dict.value" :label="dict.value">{{ dict.label
              }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="营地名称" prop="campname">
          <el-input v-model="form.campname" placeholder="请输入营地名称" />
        </el-form-item>
        <el-form-item label="营地经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入营地经度" />
        </el-form-item>
        <el-form-item label="营地纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入营地纬度" />
        </el-form-item>
        <el-form-item label="营地位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入营地位置" />
        </el-form-item>
        <el-form-item label="具体位置" prop="address">
          <el-input v-model="form.address" placeholder="请输入具体位置" />
        </el-form-item>
        <el-form-item label="营地时间类型" prop="timeType">
          <el-radio-group v-model="form.timeType">
            <el-radio v-for="dict in dict.type.camp_timetype_status" :key="dict.value" :label="dict.value">{{ dict.label
              }}</el-radio>
          </el-radio-group>
        </el-form-item>


        <el-form-item label="具体时间" prop="specificTime">
          <el-date-picker v-model.sync="form.specificTime" value-format="yyyy-MM" type="monthrange" range-separator="至" start-placeholder="开始月份"
            end-placeholder="结束月份">
          </el-date-picker>
        </el-form-item>


        <el-form-item label="联系人" prop="linkman">
          <el-input v-model="form.linkman" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="图片" prop="imageUrl">
          <image-upload v-model="form.imageUrl" />
        </el-form-item>
        <el-form-item label="营地状态" prop="state">
          <el-radio-group v-model="form.state">
            <el-radio v-for="dict in dict.type.camp_state_status" :key="dict.value" :label="dict.value">{{ dict.label
              }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="介绍" prop="introduce">
          <el-input v-model="form.introduce" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="审核状态" prop="approve">
          <el-radio-group v-model="form.approve">
            <el-radio v-for="dict in dict.type.camp_approve_status" :key="dict.value" :label="dict.value">{{ dict.label
              }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否置顶" prop="show">
          <el-radio-group v-model="form.show">
            <el-radio v-for="dict in dict.type.camp_show_status" :key="dict.value" :label="dict.value">{{ dict.label
              }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="评分" prop="score">
          <el-input v-model="form.score" placeholder="请输入评分" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCamp, getCamp, delCamp, addCamp, updateCamp } from "@/api/camp/camp";

export default {
  name: "Camp",
  dicts: ['camp_show_status', 'camp_approve_status', 'camp_timetype_status', 'camp_state_status', 'camp_camptype_status', 'camp_usertype_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 营地表格数据
      campList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        usertype: null,
        camptype: null,
        campname: null,
        longitude: null,
        latitude: null,
        location: null,
        address: null,
        timeType: null,
        specificTime: null,
        linkman: null,
        phone: null,
        imageUrl: null,
        state: null,
        introduce: null,
        approve: null,
        show: null,
        score: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        usertype: [
          { required: true, message: "运营者类型不能为空", trigger: "change" }
        ],
        camptype: [
          { required: true, message: "营地类型不能为空", trigger: "change" }
        ],
        campname: [
          { required: true, message: "营地名称不能为空", trigger: "blur" }
        ],
        longitude: [
          { required: true, message: "营地经度不能为空", trigger: "blur" }
        ],
        latitude: [
          { required: true, message: "营地纬度不能为空", trigger: "blur" }
        ],
        timeType: [
          { required: true, message: "营地时间类型不能为空", trigger: "change" }
        ],
        specificTime: [
          { required: true, message: "营地具体时间不能为空", trigger: "change" }
        ],
        state: [
          { required: true, message: "营地状态不能为空", trigger: "change" }
        ],
        introduce: [
          { required: true, message: "介绍不能为空", trigger: "blur" }
        ],
        approve: [
          { required: true, message: "审核状态不能为空", trigger: "change" }
        ],
        show: [
          { required: true, message: "是否置顶不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询营地列表 */
    getList() {
      this.loading = true;
      listCamp(this.queryParams).then(response => {
        this.campList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        campid: null,
        usertype: null,
        camptype: null,
        campname: null,
        longitude: null,
        latitude: null,
        location: null,
        address: null,
        timeType: null,
        specificTime: null,
        linkman: null,
        phone: null,
        imageUrl: null,
        state: null,
        introduce: null,
        approve: null,
        show: null,
        score: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.campid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加营地";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const campid = row.campid || this.ids
      getCamp(campid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改营地";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          const { specificTime } = this.form;
          const form = { ...this.form, specificTime: `${specificTime[0]}至${specificTime[1]}` }
          if (this.form.campid != null) {
            
            updateCamp(form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCamp(form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const campids = row.campid || this.ids;
      this.$modal.confirm('是否确认删除营地编号为"' + campids + '"的数据项？').then(function () {
        return delCamp(campids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('camp/camp/export', {
        ...this.queryParams
      }, `camp_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
